// pages/bindCode.js

const app = getApp()

const {  wxPost, wxGet, commonShare } = require('../../utils/common.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: ''
  },
  onLoad: function(options) {

  },
  codeInput: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  authCode: function() {
    if (!this.data.code) {
      wx.showToast({
        title: '请先输入Code',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '正在验证,请稍候...',
      mask: true
    })
    wxPost('/assetsInfo/authCode', { code: this.data.code, infoId: app.globalData.userId }, ({
      data
    }) => {
      if (data && data['errorCode'] === 0) {
        wx.showToast({
          title: `${data.msg}` || '验证成功',
          icon: 'success',
          duration: 2000
        })
        app.globalData.userId = data.infoId//reset userId
        var pages = getCurrentPages()
        var prevPage = pages[pages.length - 2] //上一个页面
        if (prevPage) {
          prevPage.loadAssetsInfo()
        }
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: `${data.msg || '验证失败'}`,
          icon: 'none',
          duration: 2000
        })
      }
    }, null, () => {
      wx.hideLoading()
    })
  }
})